package com.glut.forum.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.forum.bean.entity.TbArticle;
import com.glut.forum.bean.vo.ArticleVO;
import com.glut.forum.common.beans.ResultBean;
import com.glut.forum.mapper.TbArticleMapper;
import com.glut.forum.service.ITbArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-09-02
 */
@Service
@Slf4j
@Data
@AllArgsConstructor
public class TbArticleServiceImpl extends ServiceImpl<TbArticleMapper, TbArticle> implements ITbArticleService {

    /**
     * 分页返回全部文章
     *
     * @return
     */
    @Override
    public List<TbArticle> selectArticlePage(Integer idx, Integer limit) {

        log.info("分页后的全部文章, 获取第 {} 页, 页面limit: {}", idx, limit);

        // 设置默认值
        if (limit == null || limit == 0) {
            limit = 20;
        }
        // 下标为负值就会被设置为
        if (idx == null || limit <= 0) {
            idx = 1;
        }

        IPage<TbArticle> articlePage = new Page<>(idx, limit);

        List<TbArticle> records = super.page(articlePage).getRecords();

        return records;
    }
}
